package com.demo.exception;

import com.demo.utils.JSONResult;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一的异常拦截处理 Handler
 * 可以针对异常的类型进行捕获, 然后返回json对象给前端
 */

@ControllerAdvice // 结合 @ExceptionHandler 实现全局异常处理
public class GraceExceptionHandler {

    @ExceptionHandler(FileSizeLimitExceededException.class) // 异常的类型
    @ResponseBody
    public JSONResult returnMaxFileSizeLimit(FileSizeLimitExceededException e) {
        return JSONResult.errorMsg("file can not exceed 500KB");
    }

    @ExceptionHandler(MyCustomException.class)
    @ResponseBody
    public JSONResult returnMyCustomException(MyCustomException e) {
        return JSONResult.errorMsg(e.getMessage());
    }
}
